/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        resolve(res)
      }
    })
  })
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + '='
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result
}
/**
 * @description:去掉富文本内容中的标签
 * @event:
 * @return {*}
 */
export function removeTags(html) {
  return html.replace(/<[^>]+>/g, '')
}

/**
 * @description: 判断是否是微信内置浏览器打开页面
 * @event:
 * @return {*}
 */
// export function isWechatBrowser() {
//   return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger'
// }


export function buildTree(data) {
  const tree = [];
  const map = new Map();

  // 先把所有数据放入 Map，初始化 children
 data.forEach((item) => {
	 if (item.id !== "10000000") {
	   map.set(item.id, { ...item, parentId: item.parentId ? String(item.parentId) : "0", children: [] });	 
	 }
   });
  data.forEach((item) => {
      const node = map.get(item.id);
      if (!node) return;
  
      if (node.parentId === "0" || node.parentId === null) {
        // 根节点
        tree.push(node);
      } else {
        // 子节点，加入对应的父级
        const parent = map.get(node.parentId);
        if (parent) {
          parent.children.push(node);
        }
      }
    });
  
    // 对 children 按 orderNum 排序（如果需要）
    function sortTree(nodes) {
      nodes.forEach((node) => {
        if (node.children.length) {
          node.children.sort((a, b) => (Number(a.orderNum) || 0) - (Number(b.orderNum) || 0));
          sortTree(node.children);
        }
      });
    }
    sortTree(tree);
  
    return tree;
}